package com.yuzhe.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.yuzhe.model.Banner;
import com.yuzhe.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;

public class BannerController extends Controller {

    public void getBannerList() {
        String pageNumberStr = getPara("pageNumber");
        int pageNum = 0;
        if (!StringUtils.isBlank(pageNumberStr)) {
            pageNum = Integer.parseInt(pageNumberStr);
        }

        Page<Banner> paginate = Banner.dao.paginate(pageNum, 10, "select banner.*,news.title as newsTitle", "from banner left join news on banner.newsId = news.id");
        renderJson(JSONResult.ok(paginate));
    }

    public void saveBanner() {
        String title = getPara("title");
        String sort = getPara("sort");
        String text = getPara("text");
        String imagePath = getPara("imagePath");
        String newsId = getPara("newsId");
        String url = getPara("url");

        if (StringUtils.isBlank(imagePath)) {
            renderJson(JSONResult.errorMsg("轮播图片不能为空"));
            return;
        }
        if (StringUtils.isBlank(sort)) {
            sort = "50";
        }

        String id = Sid.next();
        boolean save = Banner.dao.setId(id).setTitle(title)
                .setSort(Integer.valueOf(sort)).setText(text)
                .setNewsId(newsId)
                .setUrl(url)
                .setImageUrl(imagePath).save();
        if (save) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }

    //更新banner
    public void updateBanner() {

        String title = getPara("title");
        String id = getPara("id");
        String sort = getPara("sort");
        String text = getPara("text");
        String imagePath = getPara("imagePath");
        String newsId = getPara("newsId");
        String url = getPara("url");

        if (StringUtils.isBlank(id)) {
            renderJson(JSONResult.errorMsg("id不能为空"));
            return;
        }


        if (StringUtils.isBlank(imagePath)) {
            renderJson(JSONResult.errorMsg("轮播图片不能为空"));
            return;
        }
        if (StringUtils.isBlank(sort)) {
            sort = "50";
        }

        boolean save = Banner.dao.setId(id).setTitle(title)
                .setSort(Integer.valueOf(sort)).setText(text)
                .setNewsId(newsId)
                .setUrl(url)
                .setImageUrl(imagePath).update();
        if (save) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }

    //删除轮播图
    public void deleteBanner() {
        String ids = getPara("ids");
        if (StringUtils.isBlank(ids)) {
            renderJson(JSONResult.errorMsg("轮播图id不能为空"));
            return;
        }

        String sql = "delete from banner where id in (" + ids + ")";
        int update = Db.update(sql);
        if (update > 0) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("批量删除失败"));
        }
    }
}
